package ru.ifmo.network.skype.view.swing;

import ru.ifmo.network.skype.GroupProvider;
import ru.ifmo.network.skype.UserData;
import ru.ifmo.network.skype.audio.VolumeController;
import ru.ifmo.network.skype.network.TCPClient;
import ru.ifmo.network.skype.network.data.Group;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static ru.ifmo.network.SwingUtils.getBox;
import static ru.ifmo.network.SwingUtils.getLabeledComponent;

/**
 * @author avhaliullin
 */
public class ListItemComponent extends JComponent {
    public ListItemComponent(final UserData userData, final VolumeController volumeController, final TCPClient tcpClient, final GroupProvider groupProvider) {
        final JSlider volumeControl = new JSlider(0, 100);
        volumeControl.setValue((int) (100 * volumeController.getVolume(userData)));
        setLayout(new BorderLayout());
        volumeControl.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                volumeController.setVolume(userData, 0.01f * volumeControl.getValue());
            }
        });
        JButton invite = new JButton("Invite");
        invite.setMaximumSize(invite.getPreferredSize());
        invite.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Group group = groupProvider.getGroup();
                if (group != null) {
                    tcpClient.invite(userData.getUserAddress(), group);
                }
            }
        });
        add(getBox(
                true,
                userData.getName(),
                getLabeledComponent(new JLabel(userData.getUserAddress().getIpAddress().toString()), "Address: "),
                getLabeledComponent(volumeControl, "Volume: "),
                invite));
        setMaximumSize(getPreferredSize());
    }
}
